/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeNodeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.leeloo.core.TagService;

/**
 * A dialog that displays the hierarchy of NamedTreeNodes. Any child node that
 * is an instance of {@link LabeledTagTreeNode} will be editable in the tree.
 * 
 * @see LabeledTreeNodeLabelProvider
 * @see LabeledTagTreeNodeCellModifier
 */
public class LabeledTreeNodeDialog extends Dialog {
    private final LabeledTreeNode[] nodes;

    private final String shellText;

    private final TagService tagService;

    /**
     * Constructs a new instance.
     * 
     * @param parentShell
     *            parent of the dialog shell
     * @param nodes
     *            nodes to display
     * @param shellText
     *            text to display as the Shell text
     */
    public LabeledTreeNodeDialog(Shell parentShell, LabeledTreeNode[] nodes, String shellText, TagService tagService) {
        super(parentShell);

        this.nodes = nodes;
        this.shellText = shellText;
        this.tagService = tagService;
    }

    /**
     * Constructs a TreeViewer and associated objects.
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    protected Control createDialogArea(Composite parent) {
        getShell().setText(shellText);

        Composite composite = (Composite) super.createDialogArea(parent);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite.setLayout(new FillLayout());

        TreeViewer viewer = new TreeViewer(composite, SWT.FULL_SELECTION);
        viewer.setUseHashlookup(true);

        Tree tree = viewer.getTree();
        tree.setHeaderVisible(true);
        tree.setLinesVisible(true);
        new TreeColumn(tree, SWT.NONE).setText(Messages.getString("LabeledTreeNodeDialog.name")); //$NON-NLS-1$
        new TreeColumn(tree, SWT.NONE).setText(Messages.getString("LabeledTreeNodeDialog.value")); //$NON-NLS-1$

        viewer.setContentProvider(new TreeNodeContentProvider());
        viewer.setLabelProvider(new LabeledTreeNodeLabelProvider(tagService));
        viewer.setCellEditors(new CellEditor[] { null, new TextCellEditor(tree) });
        viewer.setColumnProperties(new String[] { null, LabeledTagTreeNodeCellModifier.VALUE_PROPERTY });
        viewer.setCellModifier(new LabeledTagTreeNodeCellModifier(viewer, tagService));

        viewer.setInput(nodes);
        viewer.expandAll();

        for (int i = 0; i < tree.getColumnCount(); i++) {
            tree.getColumn(i).pack();
        }

        return composite;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#getInitialSize()
     */
    protected Point getInitialSize() {
        return new Point(400, 400);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#setShellStyle(int)
     */
    protected void setShellStyle(int newShellStyle) {
        super.setShellStyle(newShellStyle | SWT.RESIZE);
    }
}